package com.jqkb.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fasterxml.jackson.databind.JsonNode;
import com.jqkb.R;
import com.jqkb.adapters.ProccesedForwardAdapter;
import com.jqkb.adapters.SendedAdapter;
import com.jqkb.service.ActionService;
import com.jqkb.service.bean.Action;
import com.jqkb.service.util.Converter;
import com.jqkb.service.util.ListCallBack;
import com.jqkb.service.util.SimpleApi;
import com.jqkb.utils.Constant;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by alimjantursun on 18/5/29.
 */
public class FSendedFragment extends Fragment implements OnRefreshListener, OnRefreshLoadmoreListener {

    private static Context context;
    private RecyclerView recyclerView;
    private ProccesedForwardAdapter adapter;
    private RefreshLayout refreshLayout;
    private int page = 1;

    public static FSendedFragment getInstance(Context context) {
        FSendedFragment.context = context;
        return new FSendedFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_layout, container, false);
        this.recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ProccesedForwardAdapter(context);
        this.recyclerView.setAdapter(adapter);
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnRefreshLoadmoreListener(this);

        return view;
    }

    @Override
    public void onRefresh(final RefreshLayout refreshlayout) {
        ActionService service = SimpleApi.create(ActionService.class);
        Call<ResponseBody> responseBodyCall = service.actionForwardListProcessed(Constant.PAGE_SIZE, 1, "send");


        page = 1;

        Converter.convert(Action.class, responseBodyCall, new ListCallBack<Action>() {


            @Override
            public void onSuccess(Object object, JsonNode data) {


                List<Action> actions = (List<Action>) object;
                adapter.setData(actions);
                refreshlayout.finishRefresh();

//                Toast.makeText(PendingForwardActivity.this, ""+actions.size(), Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onFail(boolean success, int code, String message, JsonNode data) {
            }

            @Override
            public void onNetworkFail(Call<ResponseBody> jsonNode) {
            }

            @Override
            public void onError(Call<ResponseBody> response) {
            }
        });
    }

    @Override
    public void onLoadmore(final RefreshLayout refreshlayout) {
        ActionService service = SimpleApi.create(ActionService.class);
        Call<ResponseBody> responseBodyCall = service.actionForwardListProcessed(Constant.PAGE_SIZE, page + 1, "send");

        Converter.convert(Action.class, responseBodyCall, new ListCallBack<Action>() {


            @Override
            public void onSuccess(Object object, JsonNode data) {

                page += 1;
                List<Action> actions = (List<Action>) object;
                adapter.addData(actions);
                refreshlayout.finishLoadmore();

//                Toast.makeText(PendingForwardActivity.this, ""+actions.size(), Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onFail(boolean success, int code, String message, JsonNode data) {
            }

            @Override
            public void onNetworkFail(Call<ResponseBody> jsonNode) {
            }

            @Override
            public void onError(Call<ResponseBody> response) {
            }
        });
    }
}
